package yc.web.servlets;

import yc.bean.CartItem;
import yc.bean.Resorder;
import yc.bean.User;
import yc.web.model.JsonModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@WebServlet("/custOp.action")
public class CustServlet extends BaseServlet{
    protected void confirmOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        JsonModel jm = new JsonModel();
        //取参数 订单的参数  ， 购物车
        Resorder resorder=null;
        try {
       resorder=super.parseObjectFromRequest(req,Resorder.class);
        //调用业务层处理
            HttpSession session=req.getSession();
        Map<Integer, CartItem>cart= (Map<Integer, CartItem>) session.getAttribute("cart");
        Set<CartItem>cartItems= new HashSet<>(cart.values());
        User user = (User) session.getAttribute("user");
        OrederBiz ob=new OrederBiz();

        int result=ob.order(resorder,cartItems,user);
        jm.setCode(1);
        session.removeAttribute("cart");
        }catch (Exception e){
            e.printStackTrace();
            jm.setCode(0);
            jm.setError(e.getMessage());
        }
        super.writeJson(jm,resp);
    }
}
